package Search;

import Data.GroupData;
import Data.MassageData;
import Data.event;
import MyInterfaces.MassageType;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.*;

public class SearchGroup {
    private MassageData massageData;

    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    public event search() throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql="select *from chat_room_list where group_id=?";
        GroupData groupData=new GroupData();

        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,massageData.getReceiver());
        ResultSet resultSet=operation.executeQuery();
        event returnmassage=new event();
        if(resultSet.next())
        {
            System.out.println("有这个群聊");
            //说明有，需要再次判断是否已经在群聊里面
            String group_id=resultSet.getString("group_id");
            String path=resultSet.getString("group_avatar");
            byte[] t=null;
            System.out.println(path);
            if(path!=null)
            {
                File file=new File(path);

                if (file.exists())
                {
                    System.out.println("头像数据不为空");
                    byte[] buffer = new byte[1024*5];
                    t = new byte[(int) file.length()];
                    InputStream is = Files.newInputStream(file.toPath());
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int len;
                    while ((len = is.read(buffer)) != -1)
                    {
                        bos.write(buffer, 0, len);
                    }
                    t = bos.toByteArray();
                }
            }
            groupData.setGroup_avatar(t);

            String newsql="select *from group_identity_list where id=? and group_id=?;";
            operation=Conn.prepareStatement(newsql);
            operation.setString(1,massageData.getSender());
            operation.setString(2,massageData.getReceiver());

            ResultSet newres=operation.executeQuery();
            if(newres.next())
            {
                System.out.println("他已经存在群聊当中");
                //说明已经在群聊里面了，不可以添加
                returnmassage.setType(MassageType.Search_Group_Fail);
            }
            else
            {
                System.out.println("成功");
                //不在发送消息过去
                returnmassage.setType(MassageType.Search_Group_Success);
                groupData.setGroup_id(group_id);
            //    groupData.
            }
        }
        else
        {
            //说明没有
            returnmassage.setType(MassageType.Search_Group_Fail);
        }
        returnmassage.setT(groupData);
        return returnmassage;
    }
    public SearchGroup(MassageData massageData) {
        this.massageData = massageData;
    }
}
